<?php

namespace app\admin\service;


use think\Model;

use app\model\AdminUser;
use app\model\UserMenu;
use app\model\User as DingdingUser;


class User extends Model
{
    protected $mAdminUser;

    protected $mUserMenu;

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->mAdminUser = new AdminUser();
        $this->mUserMenu = new UserMenu();
    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 15:59
     * @description PC用户登录
     * @param string $username 用户登录账号
     * @param string $password 用户登录密码
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function confirmUserLogin($username = '', $password = '')
    {
        if (!$username) {
            return -1;
        }
        if (!$password) {
            return -2;
        }
        $user = $this->mAdminUser->where(['login_name' => $username])->find();
        if (empty($user)) {
            return -3;
        }
        if (!$user['login_name']) {
            return -1;
        }
        if ($user['user_status'] != 1) {
            return -4;
        }
        if ($user['login_password'] != md5($password) ) {
            return -5;
        }
        session('user_id', $user['id']);
        session('user_name', $user['user_name']);
        return 1;
    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 16:26
     * @description 用户修改自己账号密码
     * @param string $password_old 旧密码
     * @param string $password_new 新密码
     * @param string $password_new_re 新密码重复
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPasswordHandle($password_old = '', $password_new = '', $password_new_re = '')
    {
        if (!$password_old) {
            return -1;
        }
        if (!$password_new) {
            return -2;
        }
        if (!$password_new_re) {
            return -2;
        }
        if ($password_old == $password_new) {
            return -3;
        }
        if ($password_new_re != $password_new) {
            return -4;
        }
        $user_id = session('user_id');
        if (!$user_id) {
            return -10;
        }
        $user = $this->mAdminUser->where(['id' => $user_id])->find();
        if (empty($user)) {
            return -11;
        }
        if ($user['login_password'] != md5($password_old)) {
            return -12;
        }
        $res = $this->mAdminUser->where(['id' => $user_id])->update(['login_password' => md5($password_new)]);
        if ($res) {
            return 1;
        } else {
            return 0;
        }
    }

    /*获取用户列表*/
    public function getUserList()
    {
        $userList = $this->mAdminUser
            ->order('id asc')
            ->select();
        return $userList;
    }

    /*保存修改后密码*/
    public function savePasswordEdit($login_password, $user_id)
    {
        $user = $this->mAdminUser->where("id", $user_id)->find();
        if (!$user) {
            return 0;
        }
        $data['login_password'] = md5($login_password);
        if ($data['login_password'] === $user['login_password']) {
            return 1;
        }
        $re = $this->mAdminUser->where('id', $user_id)->update($data);
        if($re){
            return 1;
        }else{
            return 0;
        }
    }

    /*删除用户*/
    public function delUser($data)
    {
        $user = $this->mAdminUser->where("id", $data['id'])->find();
        if (!$user) {
            return 1;
        }
        $re = $this->mAdminUser->where("id", $data['id'])->delete();
        if($re){
            return 1;
        }else{
            return 0;
        }
    }

    /*获取待编辑用户信息*/
    public function getUserMsg($id)
    {
        $user = $this->mAdminUser->where("id", $id)->find();
        $user['auth'] = explode(',', $user['menu_auth']);
        return $user;
    }

    /*获取权限菜单列表*/
    public function getAuthMsg()
    {
        //获取一级菜单
        $list = collection($this->mUserMenu->where('menu_level',0)->order('id asc')->select())->toArray();
        //获取二级菜单
        foreach($list as $key => $v){
            $list[$key]['child'] = $this->mUserMenu -> where(['menu_level' => 1, 'parent_id' => $v['id']])
                ->order('id asc')
                ->select();
            $list[$key]['num'] = count($list[$key]['child']);
        }
        return $list;
    }

    /**
     * @auther zlq
     * @create_time 2020/7/16 16:08
     * @description 获取钉钉用户列表
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ddUserList()
    {
        $ddUser = new DingdingUser();
        $list = $ddUser->field('dd_user_id, name')->select();
        return $list;
    }

    /*保存编辑的员工信息*/
    public function saveEdit($data, $id){
        if($id){
            $user = $this->mAdminUser->where("login_name", $data['login_name'])->find();
            if (empty($user)) {
                return -1;
            }
            if ($user['id'] != $id) {
                return -2;
            }
            if (($data['dd_user_id'] != 0)) {
                $dd = $this->mAdminUser->where("dd_user_id", $data['dd_user_id'])->find();
                if ($dd && $dd['id'] != $id) {
                    return -3;
                }
            }
            $re = $this->mAdminUser->where('id', $id)->update($data);
            if($re){
                return 1;
            }else{
                return 0;
            }
        }else{
            $user = $this->mAdminUser-> where("login_name", $data['login_name'])->find();
            if ($user) {
                return -11;
            }
            if (($data['dd_user_id'] != 0)) {
                $dd = $this->mAdminUser->where("dd_user_id", $data['dd_user_id'])->find();
                if (!empty($dd)) {
                    return -12;
                }
            }
            $re =  $this->mAdminUser-> insert($data);
            if($re){
                return 11;
            }else{
                return -10;
            }
        }

    }
}